package com.atmc.controller;

import com.atmc.bean.Bill;
import com.atmc.bean.Code;
import com.atmc.bean.Email;
import com.atmc.service.EmailService;
import com.atmc.service.OpenFeignService;
import com.sun.prism.shader.DrawSemiRoundRect_Color_AlphaTest_Loader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class EmailFeignController {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    EmailService emailService;
    @Autowired
    OpenFeignService openFeignService;
    //远程获取email集合数据 视图渲染
    @RequestMapping("/email/toemailpage")
    public String toemailpage(Model model){
        List<Email> list = openFeignService.emaillist();
        model.addAttribute("msg",list);
        return "email/email";
    }
    //删除一条账号记录
    @RequestMapping("/toemaildelete/{email_id}")
    public String emaildeleteOne(@PathVariable("email_id")Integer email_id, Model model){
        emailService.deleteOne(email_id);

        List<Email> list = openFeignService.emaillist();
        model.addAttribute("msg",list);

        return "email/email";
    }
    //跳转到修改密码页面
    @RequestMapping("/toemailupdate/{email_add}")
    public String toUpdateCode(@PathVariable("email_add")String email_add,Model model){
        model.addAttribute("email_add",email_add);
        return "email/emailUpdate";
    }
    //修改一条邮箱记录
    @RequestMapping("/toemailupdatesuccess")
    public String tocodeupdatesuccess(@RequestParam("email_source")String email_source,
                                      @RequestParam("email_title")String email_title,
                                      @RequestParam("email_add")String email_add
            ,Model model){
        Email email=new Email(null,email_source,email_title,email_add,null,null,null);
        emailService.updateOne(email);
        List<Email> list = openFeignService.emaillist();
        model.addAttribute("msg",list);
        return "email/email";
    }
    @RequestMapping("/toemailaddpage")
    public String toemailaddpage(){
        return "email/emailAdd";
    }
    //添加一条账号记录
    @RequestMapping("/toinsertemail")
    public String toinsertcode(@RequestParam("email_source")String email_source,
                               @RequestParam("email_title")String email_title,
                               @RequestParam("email_add")String email_add,
                               Model model
    ){
        Email email=new Email(null,email_source,email_title,email_add,null,null,null);
        emailService.insertOne(email);
        List<Email> list = openFeignService.emaillist();
        model.addAttribute("msg",list);
        return "bill/insertsuccess";
    }
    //查询一条邮箱记录
    @RequestMapping("/email/selectOne")
    public String selectOne(@RequestParam("email_add")String email_add, Model model){
        Email list = emailService.selectOne(email_add);
        model.addAttribute("msg",list);
        return "email/email";
    }
    //去跳转到发送邮件的页面
    @RequestMapping("/tosendmessage")
    public String tosendmessage(){
        return "email/sendmessage";
    }
    //去发送邮箱
    @RequestMapping("/sendersuccess")
    public String sendersuccess(@RequestParam("sender")String sender,
                                @RequestParam("email_source")String email_source,
                                @RequestParam("email_add")String email_add,
                                @RequestParam("email_title")String email_title,
                                @RequestParam("email_content")String email_content,
                                Model model
                                ){
        Email email=new Email(null,email_source,email_title,email_add,null,null,email_content);
        emailService.insertAn(email);

        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(sender+"(小甜甜)");
        mailMessage.setTo(email_add);
        mailMessage.setSubject(email_title);
        mailMessage.setText(email_content);
        javaMailSender.send(mailMessage);
        System.out.println(sender+"==="+email_source+"==="+email_add+"==="+email_title+"==="+email_content);
        List<Email> list = openFeignService.emaillist();
        model.addAttribute("msg",list);
        return "email/sendsuccess";
    }
    /*单文件上传
    * 可以直接 filepic.transferTo() 转储到指定的位置，如磁盘位置。
    * 多文件上传
    * for循环遍历打印输出文件
    *  for (MultipartFile file : files) {
            file.getOriginalFilename();
        }
    *
    * */
    @RequestMapping("/uploadfile")
    public String uploadfile(@RequestPart("filepic")MultipartFile filepic)  {
        if(filepic.isEmpty()){
            return "html/one_set";
        }
        //单文件上传
        String contentType = filepic.getContentType();
        String name = filepic.getName();
        String originalFilename = filepic.getOriginalFilename();
        try {
            byte[] bytes = filepic.getBytes();
            System.err.println(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.err.println(contentType+"\n" +name+"\n"+originalFilename);
        InputStream is = null;
        FileOutputStream fos= null;
        try {
            is = filepic.getInputStream();
            fos = new FileOutputStream(new File("D:\\IdeaProjects\\kinggroup524\\cloud-consumer81\\src\\main\\resources\\static\\img\\"+filepic.getOriginalFilename()));
            byte[] buff=new byte[5];
            int len;
            while((len=is.read(buff)) != -1){
                fos.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null){

                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos !=null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "bill/insertsuccess";
    }
}
